<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author: 冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace common\extend\exception;

use yii\base\UserException;
use yii\web\Response;

class B5HttpException extends UserException
{
    /**
     * @var int HTTP status code, such as 403, 404, 500, etc.
     */
    public int $statusCode;

    public function __construct(int $status, $message = null, $code = 0, $previous = null)
    {
        $this->statusCode = $status;
        parent::__construct((string)$message, $code, $previous);
    }

    /**
     * 获取错误，先从自定义错误开始查找
     * @return string
     */
    public function getName(): string
    {
        $error = ErrorCode::getError($this->statusCode, '');
        return $error ?: (Response::$httpStatuses[$this->statusCode] ?? 'Error');
    }
}